package com.sgu6.sgu6_machine;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.util.Log;

import androidx.multidex.MultiDex;

import com.danikula.videocache.HttpProxyCacheServer;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.db.DBManager;
import com.sgu6.sgu6_machine.dto.MachineConfigDto;
import com.sgu6.sgu6_machine.handler.UnCeHandler;
import com.sgu6.sgu6_machine.util.VideoCacheUtil;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.ArrayList;
import java.util.List;

public class App extends Application {

    private static final String TAG = App.class.getSimpleName();
    private HttpProxyCacheServer proxy;
    private List<Activity> list = new ArrayList<>();
    private MachineConfig machineConfig = new MachineConfig();

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //设置该CrashHandler为程序的默认处理器，异常退出重启
        UnCeHandler catchExcep = new UnCeHandler(this);
        Thread.setDefaultUncaughtExceptionHandler(catchExcep);
        //bugly错误日志采集
        CrashReport.initCrashReport(getApplicationContext(), "fece1b7cad", false);
        Logger.addLogAdapter(new AndroidLogAdapter());
        //加载机器配置信息
        loadConfig(this);
    }


    public static void loadConfig(Context context) {
        try {
            List<MachineConfigDto> machineConfigDtos = DBManager.getInstance(context).queryMachineInfoList();
            if (machineConfigDtos.size() == 0) {
                MachineConfig machineConfig = new MachineConfig();
                MachineContainer.machineConfigDto = machineConfig.loadConfig(context);
            } else {
                MachineContainer.machineConfigDto = machineConfigDtos.get(0);
            }
        } catch (Exception e) {
            Log.i(TAG, "loadConfig: ");
        }

    }

    public static HttpProxyCacheServer getProxy(Context context) {
        App app = (App) context.getApplicationContext();
        return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
    }

    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer.Builder(this)
                .cacheDirectory(VideoCacheUtil.getVideoCacheDir(this))
                .build();
    }


    /**
     * Activity关闭时，删除Activity列表中的Activity对象
     */
    public void removeActivity(Activity a) {
        list.remove(a);
    }

    /**
     * 向Activity列表中添加Activity对象
     */
    public void addActivity(Activity a) {
        list.add(a);
    }

    /**
     * 关闭Activity列表中的所有Activity
     */
    public void finishActivity() {
        for (Activity activity : list) {
            if (null != activity) {
                activity.finish();
            }
        }
        //杀死该应用进程
        android.os.Process.killProcess(android.os.Process.myPid());
    }


}
